#! /usr/bin/env python3
#! coding: utf-8
#! python3

import os
import argparse
import json

#
def compare_json( originalFile=None, copyFile=None ):
	score = 0
	totalItems = 0
	
	if originalFile != None :
		oriItems = {}
		with open( originalFile, "r", encoding="utf8" ) as oriFile :
			oriItems = json.load( oriFile )
		
		copyItems = {}
		with open( copyFile, "r", encoding="utf8" ) as cFile :
			copyItems = json.load( cFile )
		
		for item in oriItems :
			totalItems += 1
			
			if item in copyItems :
				if copyItems[item] != "" :
					# If an item exists but have a different content, it's fair to count it as translated.
					if copyItems[item] != oriItems[item] :
						score += 1
			
	
	print( f"{score}/{totalItems} ({((score/totalItems)*100):3.1f}%)" )
	return score

#==============================================================================
# Main program
#==============================================================================

appPath = os.path.dirname( os.path.realpath( __file__ ) )
projectPath = os.path.join( appPath, ".." )

#--------------------
# Load the indexes conversion
confPath = os.path.join( appPath, "config.json")
if not os.path.exists( confPath ) :
	print( f"'{confPath}' don't exist. Please create one." )
	exit( 1 )

with open( confPath, "r" ) as file :
	config = json.load( file )


#--------------------
# Check config

#--------------------
# Default source material
if "default" not in config :
	print( "No default path to check. Please add one" )
	exit( 10 )
	

defaultPath = config["default"]

if not os.path.exists( defaultPath ) :
	print( "Default path don't exists. Please create it." )
	exit( 11 )
	

if not os.path.isdir( defaultPath ) :
	print( "Default path is not a directory. Please create it." )
	exit( 12 )

print( f"Default: {defaultPath}" )

if "material" not in config :
	print( "No material liste for translation. Please add some" )
	exit( 13 )
	

filesToCheck = []

for file in config["material"] :
	defaultFile = os.path.join( defaultPath, file )
	
	if not os.path.exists( defaultFile ) :
		print( f"'{file}' Don't exist. Please create it." )
		exit( 14 )
	
	if os.path.isfile( defaultFile ) :
		filesToCheck.append( file )
	
	if os.path.isdir( defaultFile ) :
		for f in os.listdir( defaultFile ) :
			p = os.path.join( defaultFile, f )
			if os.path.isfile( p ) :
				filesToCheck.append( os.path.join( file, f ) )
			else :
				print( f"'{p}' not a file. Won't be checked, can't do recursive folder." )
			

totalNumberOfFiles = len(filesToCheck)
print( f"{totalNumberOfFiles} files to check." )

#TODO for pete sake, count the correct number of items...
totalNumberOfItems = 1049
print( f"{totalNumberOfItems} items to check." )

#--------------------
# Locales
if "locale" not in config :
	print( "No locale folder list. Please, make one" )
	exit( 20 )
	

for locale in config["locale"] :
	
	if locale == config["default"] :
		print( "The default is in the locales. It can't be checked against itself, please remove." )
		exit( 21 )
		
	
	if not os.path.exists( locale ) :
		print( f"Locale '{locale}' don't exist, creating folder." )
		os.mkdir( locale )
	
	if not os.path.isdir( locale ) :
		print( f"Locale '{locale}' is not a folder. Please, put files in folder." )
		exit( 21 )
	

for locale in config["locale"] :
	
	print( f"Checking '{locale}' ..." )
	goodFiles = 0
	goodEntries = 0
	
	for file in filesToCheck :
		
		localeFile = os.path.join( locale, file )
		# print( file )
		
		if os.path.exists( localeFile ) :
			if os.path.isfile( localeFile ) :
				# print( f"'{localeFile}' Ok." )
				
				if os.path.splitext( localeFile )[1] == ".json" :
					print( localeFile+"\t", end="" )
					goodEntries += compare_json( os.path.join( defaultPath, file ), localeFile )
				else :
					goodEntries += 1
				
				goodFiles += 1
			
		
	if goodFiles == 0 :
		print( " -> No files to test" )
	else :
		print( f" -> {goodFiles}/{totalNumberOfFiles} ({((goodFiles/totalNumberOfFiles)*100):3.1f}%) file translated" )
		print( f" -> {goodEntries}/{totalNumberOfItems} ({((goodEntries/totalNumberOfItems)*100):3.1f}%) items translated" )
	
	print()
